package com.yibo.message.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yibo.db.message.entity.MessageCheckWord;
import com.yibo.db.message.mapper.MessageCheckWordMapper;
import com.yibo.message.service.MessageCheckWordService;
import com.yibo.message.service.cmd.checkword.SendCheckWordFromEmailCommand;
import com.yibo.support.helper.ObjectHelper;
import com.yibo.support.helper.TimeHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.yibo.message.constant.Constant.EMAIL;
import static com.yibo.message.constant.Constant.TYPE;

@Service
public class MessageCheckWordServiceImpl implements MessageCheckWordService {

    @Resource
    private MessageCheckWordMapper messageCheckWordMapper;

    @Override
    public int send(String email,String type) {
        return new SendCheckWordFromEmailCommand(email,type).execute();
    }

    @Override
    public String emailCheckWord(String email, String type) {
        List<MessageCheckWord> checkWordList = messageCheckWordMapper.selectList(new EntityWrapper<MessageCheckWord>().eq(EMAIL,email)
                .eq(TYPE,type));

        if (!ObjectHelper.isEmpty(checkWordList)) {
            MessageCheckWord word = checkWordList.get(0);
            String createTime = word.getCreateTime();
            //注册验证码有效期5分钟
            if("register".equals(word.getType())
                    && TimeHelper.betweenSeconds(createTime, TimeHelper.getNow()) > 300) {
                return null;
            }

            //登陆密码1分钟
            if("login".equals(word.getType())
                    && TimeHelper.betweenSeconds(createTime, TimeHelper.getNow()) > 60){

            }
            return word.getCheckWord();
        }
        return null;
    }



    @Override
    public int removeCheckWord(String email, String type) {
        return messageCheckWordMapper.delete(new EntityWrapper<MessageCheckWord>().eq(EMAIL,email)
                  .eq(TYPE,type));
    }
}
